<?php

class Soul_Util_Array {
	
	public static function get(array $arr, $key, $default = NULL){
		if(array_key_exists($key, $arr)){
			return $arr[$key];
		}
		return $default;
	}
	
	public static function removeItem(array &$arr, $key){
		if(isset($arr[$key])){
			$result = $arr[$key];
			unset($arr[$key]);
			return $result;
		}
		return NULL;
	}

    public static function removeEmpty(array &$arr){
		foreach($arr as $key=>$item){
            if(empty($item)){
                unset($arr[$key]);
            }else if(is_array($item)){
            	$arr[$key] = self::removeEmpty($item);
            }
        }
        return $arr;
    }
    
    public static function removeValue(array &$arr, $value){
    	$values = self::pack($value);
    	foreach($values as $removeValue){
    		foreach($arr as $index=>$item){
    			if($item == $removeValue){
    				$result = $arr[$index];
    				unset($arr[$index]);
    				break;
    			}
    		}
    	}
    	return $result;
    }

    public static function compareArray(array $arr1, array $arr2){
        if(count($arr1) != count($arr2)){
            return FALSE;
        }
        
        foreach($arr1 as $k=>$v1){
            if(!array_key_exists($k, $arr2)){
                return FALSE;
            }
            $v2 = $arr2[$k];
            if(is_array($v1) && is_array($v2)){
                if(!self::compareArray($v1, $v2)){
                    return FALSE;
                }
            }

            if($v1 !== $v2){
                return FALSE;
            }
        }

        return TRUE;
    }
    
    public static function pack($value){
    	if(is_array($value)){
    		return $value;
    	}
//    	if($value instanceof ArrayAccess){
//    		return $value;
//    	}
    	return array($value);
    }
}